<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['Productionline:hbsb:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['Productionline:hbsb:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['Productionline:hbsb:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['Productionline:hbsb:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="更新时间" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{scope.row.status==0?'审核中':'已审核'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline:hbsb:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-check" v-show="scope.row.status!=1"
            @click="handleApprove(scope.row)" v-hasPermi="['Productionline:hbsb:approve']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline:hbsb:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改光氧催化废气处理环保设备使用、维修记录对话框 -->
    <el-dialog v-myFullscreen append-to-body fullscreen :title="title" :visible.sync="open" width="900px" append-to-body
       v-myDialogDrag>
      <div class="mainTitle">光氧催化废气处理环保设备使用、维修记录</div>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新时间" prop="date">
              <el-date-picker clearable size="small" v-model="form.date" type="date" value-format="yyyy-MM-dd"
                placeholder="选择更新时间" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status" :disabled="openDisabled">
                <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQualityLabCapacitySub"
              :disabled="openDisabled">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQualityLabCapacitySub"
              :disabled="openDisabled">删除</el-button>
          </el-col>
        </el-row>
        <el-divider content-position="center"></el-divider>
        <el-table :data="subList" :row-class-name="rowQualityLabCapacitySubIndex" border
          @selection-change="handleQualityLabCapacitySubSelectionChange" ref="qualityLabCapacitySub">
          <el-table-column align="center" type="selection" width="50" />
          <!-- <el-table-column align="center" label="序号" prop="index" width="50" /> -->
          <el-table-column align="center" label="日期" prop="date">
            <template slot-scope="scope">
              <el-date-picker :editable="false" clearable v-model="scope.row.date" type="date" value-format="yyyy-MM-dd"
                placeholder="选择使用日期" :disabled="openDisabled">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="开机时间" align="center">
            <el-table-column align="center" label="上午" prop="openAm">
              <template slot-scope="scope">
                <el-time-picker :editable="false" clearable v-model="scope.row.openAm" placeholder="选择使用日期"
                  :disabled="openDisabled">
                </el-time-picker>
              </template>
            </el-table-column>
            <el-table-column align="center" label="下午" prop="openPm">
              <template slot-scope="scope">
                <el-time-picker :editable="false" clearable v-model="scope.row.openPm" placeholder="选择使用日期"
                  :disabled="openDisabled">
                </el-time-picker>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="停机时间" align="center">
            <el-table-column align="center" label="上午" prop="closeAm">
              <template slot-scope="scope">
                <el-time-picker :editable="false" clearable v-model="scope.row.closeAm" placeholder="选择使用日期"
                  :disabled="openDisabled">
                </el-time-picker>
              </template>
            </el-table-column>
            <el-table-column align="center" label="下午" prop="closePm">
              <template slot-scope="scope">
                <el-time-picker :editable="false" clearable size="small" v-model="scope.row.closePm"
                  placeholder="选择使用日期" :disabled="openDisabled">
                </el-time-picker>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="主要检查内容" align="center">
            <el-table-column align="center" label="管道是否密封、有无漏气">
              <template slot-scope="scope">
                <el-input v-model="scope.row.a1" placeholder="请输入" :disabled="openDisabled" />
              </template>
            </el-table-column>
            <el-table-column align="center" label="UV 光解灯镇流器指示灯工作状态是否正常。">
              <template slot-scope="scope">
                <el-input v-model="scope.row.a2" placeholder="请输入" :disabled="openDisabled" />
              </template>
            </el-table-column>
            <el-table-column align="center" label="风机是否运转平稳，有无异响、剧烈震动">
              <template slot-scope="scope">
                <el-input v-model="scope.row.a3" placeholder="请输入" :disabled="openDisabled" />
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column align="center" label="记录人">
            <template slot-scope="scope">
              <el-input v-model="scope.row.a4" placeholder="请输入" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="维修保养项目">
            <template slot-scope="scope">
              <el-input v-model="scope.row.a5" placeholder="请输入" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="维保人签字">
            <template slot-scope="scope">
              <el-input v-model="scope.row.a6" placeholder="请输入" :disabled="openDisabled" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled||openApprove">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
  import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/quality/productionline/record";

  export default {
    name: "Record",
    data() {
      return {
        //弹窗是否禁止编辑
        openDisabled: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 光氧催化废气处理环保设备使用、维修记录表格数据
        recordList: [],
        // 光氧催化废气处理环保设备使用、维修记录子表格数据
        subList: [],
        // 子表选中数据
        checkedQualityLabCapacitySub: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '请选择日期', trigger: 'blur' }
          ],
        },
        openApprove: false
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询光氧催化废气处理环保设备使用、维修记录列表 */
      getList() {
        this.loading = true;
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          date: null,
          status: 0,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null,

        };
        this.subList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.openDisabled = false;
        this.openApprove = false;
        this.open = true;
        this.title = "添加光氧催化废气处理环保设备使用、维修记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.openDisabled = false;
        this.openApprove = false;
        const id = row.id || this.ids
        getRecord(id).then(response => {
          this.form = response.data;
          for (let i in response.data.subList) {
            response.data.subList[i].a6 = this.$store.state.user.nickName
          }
          this.subList = response.data.subList;
          this.open = true;
          this.title = "修改光氧催化废气处理环保设备使用、维修记录";
        });
      },
      /** 审核按钮操作 */
      handleApprove(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids
        this.openApprove = true;
        getRecord(id).then(response => {
          response.data.status = 1;
          this.form = response.data;
          for (let i in response.data.subList) {
            response.data.subList[i].a6 = this.$store.state.user.nickName
          }
          this.subList = response.data.subList;
          this.open = true;
          this.title = "审核光氧催化废气处理环保设备使用、维修记录";
        });
      },

      /** 查看按钮 */
      handleView(row) {
        this.reset();
        this.openDisabled = true;
        this.openApprove = false;
        const id = row.id || this.ids
        getRecord(id).then(response => {
          this.form = response.data;
          for (let i in response.data.subList) {
            response.data.subList[i].a6 = this.$store.state.user.nickName
          }
          this.subList = response.data.subList;
          this.open = true;
          this.title = "查看光氧催化废气处理环保设备使用、维修记录";
        });
      },
      /** 序号 */
      rowQualityLabCapacitySubIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 添加按钮操作 */
      handleAddQualityLabCapacitySub() {
        let obj = {
          a1: "",
          a2: "",
          a3: "",
          a4: this.$store.state.user.nickName,
          a5: "",
          a6: "",
          closePm: '',
          closeAm: '',
          openPm: '',
          openAm: '',
          date: '',
        };
        this.subList.push(obj);
      },
      /** 删除按钮操作 */
      handleDeleteQualityLabCapacitySub() {
        if (this.checkedQualityLabCapacitySub.length == 0) {
          this.$alert("请先选择要删除的数据", "提示", {
            confirmButtonText: "确定",
          });
        } else {
          this.subList.splice(
            this.checkedQualityLabCapacitySub[0].index - 1,
            1
          );
        }
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          let form = { ...this.form, ...{ subList: this.subList } };
          if (valid) {
            if (this.form.id != null) {
              updateRecord(form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              const rLoading = this.openLoading();
              addRecord(form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              }).catch((error) => {
                rLoading.close();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除光氧催化废气处理环保设备使用、维修记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/record/export', {
          ...this.queryParams
        }, '光氧催化废气处理环保设备使用、维修记录' + this.parseTime(new Date) + '.xlsx')
      },
      /** 单选框选中数据 */
      handleQualityLabCapacitySubSelectionChange(selection) {
        if (selection.length > 1) {
          this.$refs.qualityLabCapacitySub.clearSelection();
          this.$refs.qualityLabCapacitySub.toggleRowSelection(selection.pop());
        } else {
          this.checkedQualityLabCapacitySub = selection;
        }
      },
    }
  };
</script>
<style>
  .mainTitle {
    width: 100%;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 30px;
  }
</style>
